
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Scanner;
public class User {
    private String userName ;
    private String userPassword ;
    private String ID;
    private String level;
    private String date;
    private double money;
    private String phone;
    private String email;
    public User(){}

    public User(String name, String password) {
        this.userPassword = password;
        this.userName = name;
    }
    public User(String ID,String userName,String level,String date,double money,String phone,String email){
        this.ID=ID;
        this.userName = userName;
        this.level = level;
        this.date = date;
        this.money = money;
        this.phone = phone;
        this.email = email;
    }
    public void register(){
        Tools tools=new Tools();
        Scanner reader=new Scanner(System.in);
        System.out.println("请输入您的邮箱：");
        this.setEmail(reader.next());
        System.out.println("请输入您的用户名：");
        System.out.println("PS:用户名长度不少于5个字符");
        String username=reader.next();
        if(username.length()<5){
            System.out.println("您的用户名不符合要求，请重新输入！");
            register();
        }
        else{
            this.setName(username);
            System.out.println("您的用户名设置成功！");
        }
        while(true){
            System.out.println("请设置您的密码：");
            System.out.println("PS:密码长度大于8个字符，必须是大小写字母、数字和标点符号的组合。");
            String password=reader.next();
            if((password.length()>8)&&(tools.hasNumber(password))&&(tools.hasBigletter(password))){
                this.userPassword=password;
                System.out.println("您的密码设置成功！");
                break;
            }
            else{
                System.out.println("密码设置失败，请重新输入！");
            }
        }
    }
    public void Login(ArrayList<User>userList){
        Scanner reader=new Scanner(System.in);
        System.out.println("请输入您的用户名：");
        String username=reader.next();
        int m=0;
        while(true) {
            System.out.println("请输入您的密码：");
            String password = reader.next();
            for (User user : userList) {
                if (username.equals(user.getName())) {
                    int index = userList.indexOf(user);
                    if (password.equals(userPassword)) {
                        System.out.println("恭喜您，您已成功登录！");
                        return;
                    } else {
                        System.out.println("密码错误，请重新输入!");
                        m++;
                        if(m==5){
                            System.out.println("您的账户暂时被冻结，请二十分钟后再重新登陆！");
                            return;
                        }
                    }
                }
            }
        }
    }
    public void changePassword(ArrayList<User>userList){
        Tools tools=new Tools();
        System.out.println("请输入旧密码：");
        Scanner reader = new Scanner(System.in);
        String oldpassword=reader.next();
        for (User user : userList) {
            if(oldpassword==user.getPassword()){
                System.out.println("请输入新密码：");
                String newPassword=reader.next();
                if((newPassword.length()<8)&&(tools.hasNumber(newPassword))&&(tools.hasBigletter(newPassword))) {
                    user.setPassword(newPassword);
                    System.out.println("用户密码已重置");
                }
            }
            else {
                System.out.println("旧密码输入错误");
            }
        }
    }
    public void forgetPassword(ArrayList<User>userList){
        Tools tools=new Tools();
        Scanner reader=new Scanner(System.in);
        System.out.println("请输入您的用户名：");
        String username=reader.next();
        SecureRandom random=new SecureRandom();
        int randomNumber=random.nextInt(10000000);
        String newpassword = String.valueOf(randomNumber);
        for(User user:userList){
            if(username.equals(user.getName())){
                System.out.println("已向您所绑定的邮箱发送登陆密码！");
                try {
                    tools.sendEmail(newpassword);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                userPassword=newpassword;
               return;
            }
        }
    }
    public void setName(String name) {this.userName = name;}
    public String getName() {return userName;}
    public void setPassword(String password) {this.userPassword= password;}
    public String getPassword() {return userPassword;}
    public String getEmail() {return email;}
    public void setEmail(String email) {this.email = email;}
    public String getPhone() {return phone;}
    public void setPhone(String phone) {this.phone = phone;}
    public double getMoney() {return money;}
    public void setMoney(double money) {this.money = money;}
    public String getDate() {return date;}
    public void setDate(String date) {this.date = date;}
    public String getLevel() {return level;}
    public void setLevel(String level) {this.level = level;}
    public String getID() {return ID;}
    public void setID(String ID) {this.ID = ID;}

}
